package com.example.musictutor.services;

import java.util.ArrayList;
import java.util.List;

import com.example.musictutor.domain.Note;
import com.example.musictutor.domain.SongNote;
import com.example.musictutor.services.listeners.OnKeyTouchedListener;
import com.example.musictutor.views.KeyButton;

public class KeyBoard {

	private static KeyBoard instance;
	private List<KeyButton> mKeys;

	private KeyBoard() {
		mKeys = new ArrayList<KeyButton>();
	}

	public static KeyBoard getInstance() {
		if (instance == null)
			instance = new KeyBoard();

		return instance;
	}

	public void setEnabled(boolean enabled) {
		for(KeyButton button :  mKeys) {
			button.setEnabled(enabled);
		}
	}
	
	public void addKey(KeyButton key)
	{
		key.setOnTouchListener(new OnKeyTouchedListener());
		mKeys.add(key);
	}
	
	public void addKey(KeyButton key, Note note)
	{
		key.setOnTouchListener(new OnKeyTouchedListener());
		key.setNote(note);
		mKeys.add(key);
	}
	
	public List<KeyButton> getKeys() {
		return mKeys;
	}

	public void setKeys(List<KeyButton> mKeys) {
		this.mKeys = mKeys;
	}
	
	public KeyButton getKeyButton(SongNote note) {
		KeyButton keyButton = null;
		for(KeyButton button :  mKeys) {
			if(button.getNote().getName().equals(note.getName())) {
				keyButton = button;
				break;
			}
		}
		
		if(keyButton == null)
			throw new NullPointerException();
		
		return keyButton;
	}
}
